using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ebookreader
{
    public static class ScreenManager
    {
        public static bool first_transition = true;

        public static void CleanupComponents(Game game)
        {
            TransitionScreen transition = null;
            if (first_transition != true)
                transition = new TransitionScreen(game);
            for (int i = 0; i < game.Components.Count; i++)
            {
                ((GameComponent)game.Components[i]).Dispose();
                i--;
            }
            //game.Components.Clear();
            if (first_transition != true)
            {
                game.Components.Add(transition);

                transition.OnTransitionStart += () =>
                {
                    for (int i = 0; i < game.Components.Count; i++)
                    {
                        GameComponent gc = (GameComponent)game.Components[i];
                        if (gc.GetType() != typeof(TransitionScreen) )//&& gc.GetType() != typeof(LoadingScreen)) // 
                            gc.Enabled = false;
                    }
                };

                transition.OnTransitionEnd += () =>
                {
                    //var loadingScreenPresence = false;

                    //for (int i = 0; i < game.Components.Count; i++)
                    //    if (game.Components[i].GetType() == typeof(LoadingScreen)) loadingScreenPresence = true;

                    //if (!loadingScreenPresence)
                        for (int i = 0; i < game.Components.Count; i++)
                        {
                            GameComponent gc = (GameComponent)game.Components[i];
                            if (gc != transition)
                                gc.Enabled = true;
                        }
                };
            }

            first_transition = false;
        }
        
        public static void SelectMainMenu(Game game)
        {
            ScreenManager.CleanupComponents(game);
            game.Components.Add(new ERMenu(game));
            
        }
        public static void StartBookReader(Game game, SpriteFont font, Book path)
        {
            ScreenManager.CleanupComponents(game);

            var reader = new Reader(game, path);
            game.Components.Add(reader);
            //game.Components.Add(new ReaderUI(game,reader));
        }
        public static void StartBookReader(Game game, Book path)
        {
            ScreenManager.CleanupComponents(game);

            var reader = new Reader(game, path);
            game.Components.Add(reader);
            //game.Components.Add(new ReaderUI(game, reader));
        }
        public static void StartTitleList(Game game, SpriteFont font)
        {
            ScreenManager.CleanupComponents(game);

            game.Components.Add(new BookSelection(game, font));
        }

        public static void StartOptionScreen(Game game)
        {
            ScreenManager.CleanupComponents(game);
            game.Components.Add(new OptionMenu(game));
        }

        public static void StartCreditsScreen(Game game)
        {
            ScreenManager.CleanupComponents(game);
            game.Components.Add(new CreditsMenu(game));
        }

        public static void StartDownloadManager(Game game, SpriteFont font)
        {
            ScreenManager.CleanupComponents(game);
            game.Components.Add(new DownloadBook(game));
        }

        public static List<Advertisement.GameTitle> advertisemets = new List<Advertisement.GameTitle>()
        {
            Advertisement.GameTitle.Dice3D,
            Advertisement.GameTitle.Enigma,
            Advertisement.GameTitle.Flashlight,
            Advertisement.GameTitle.Ideopuzzle,
            Advertisement.GameTitle.RisikoDice,
            Advertisement.GameTitle.SpaceBattle,
            Advertisement.GameTitle.ChainingExplosion,
            Advertisement.GameTitle.InformaTreni
        };

        public static void ExitGame(Game game)
            {
                CleanupComponents(game);
                if (EbookReader.isTrial)
                    game.Components.Add(new Advertisement.AdsComponent(game,advertisemets, 4));
                else
                    game.Exit();
            }
    }
}
